#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from gi.repository import Gtk, Gio
import sys
import argparse


class CherryTreeApp(Gtk.Application):
    def __init__(self):
        """
        For convenience, the restrictions on application identifiers are reproduced here:
        """
        Gtk.Application.__init__(self, application_id="apps.test.helloworld", flags=Gio.ApplicationFlags.HANDLES_OPEN)

        self.mainwindow_builder = Gtk.Builder()  # main window builder which holds the objects from the glade file
        # Property values below
        self.mainwindow = None  # the main window object when it is created
        self.program_path = sys.argv  # the program path file
        self.arguments = None  # args from the command promt
        print("Filename :", self.program_path)  # print out the flag (Debug)

    def do_startup(self):
        """
        Gtk.Application callback - sets up the application when it first starts
        """
        print("do_startup method called")
        Gtk.Application.do_startup(self)  # call the application main method
        try:
            # load the main window object in to the builder
            #self.mainwindow_builder.add_from_file("./gui_gtk3/resources/cherrytree-gtk3.glade")
            print("loaded glade file")
        except IOError as e:  # the glade file did not load so print out error and exit program
            print(e)  # print error to standard output
            sys.exit(2)  # exit the program

    def do_activate(self, data=None):
        print("do_activate method called")

        self.mainwindow = Gtk.ApplicationWindow()
        app.add_window(self.mainwindow)
        self.mainwindow.show()

    def do_open(self, files, *hint):
        """
        Gtk.Application callback -
        opens files and shows them in a new window.
        This corresponds to someone trying to open a document
        (or documents) using the application from the file browser, or similar.
        :param hint:
        :param files: Gfiles are passed
        :return:
        """
        print("do_open method called")
        print(self)
        print(files)
        print(hint)


    def do_command_line(self, args):
        """
        Gtk.Application command line handler
        called if Gio.ApplicationFlags.HANDLES_COMMAND_LINE is set.
        must call the self.do_activate() to get the application up and running.
        """
        print("do_command_line method called")
        Gtk.Application.do_command_line(self, args)  # call the default commandline handler

        # make a command line parser
        parser = argparse.ArgumentParser(prog='gui')  # add a -c/--color option
        parser.add_argument('-c', '--color', action='store_true')  # parse the command line stored in args, but skip the first element (the filename)
        print(args.get_arguments()[0:])
        self.arguments = parser.parse_args(args.get_arguments()[1:])
        print(self.args)
        self.do_startup()  # call the main program do_activate() to start up the app
        return 0

    def do_shutdown(self):
        """
        Gtk.Application callback - performs shutdown tasks
        :return:
        """
        print("do_shutdown method called")
        Gtk.Application.do_shutdown(self)  # shut the app down

# the start point of this program
if __name__ == '__main__':
    app = CherryTreeApp()  # create the app object
    exit_status = app.run(sys.argv)  # run the app
    sys.exit(exit_status)  # exit the program